import './assets/main.css'
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import { createPinia } from 'pinia'
import { router } from '@renderer/routers'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import i18nConfig from '@renderer/assets/i18n'
import { db } from '@renderer/store/db/db'
import { initStore } from '@renderer/store/pinia'
import { useSystemStore } from '@renderer/store/pinia/system'
import API from '@renderer/api/constant'
import { post } from '@renderer/api/http'
import { dev_check_login_status, IS_OPEN_AUTHORITY, shareWindowSetting } from '@renderer/config'
import routeConstants from '@renderer/routers/routeConstants'

db.init()

const pinia = createPinia()
const app = createApp(App)
app.use(pinia)

app.use(router)
app.use(ElementPlus)

app.use(i18nConfig)
app.mount('#app')

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}
initStore()

router.push({
  name: routeConstants.MprPage.name,
  // name: routeConstants.LoginPage.name,
  // name: routeConstants.MultiScanSeriesPage.name,
}).then((res) => {
  console.log(res)
})
  .catch((error) => {
    console.log(error)
  })

const startGetDiskInfo = async () => {
  await getBEDiskInfo()
  await getDiskInfo()
}

const getBEDiskInfo = async () => {
  const systemStore = useSystemStore()
  const res: any = await post(API.getDiskUseList)
  const res1: any = await post(API.getDiskAlertList)
  let obj = res1.data
  obj[0].diskName = '控制盒'
  for (let i = 1; i < obj.length; i++) {
    Object.assign(obj[i], {
      diskName: res.data[i - 1].diskName,
      usedInfo: res.data[i - 1].usedInfo
    })
  }
  systemStore.setDiskInfo(obj)
}

const getDiskInfo = async () => {
  const systemStore = useSystemStore()
  const response = await (window as any).electron.ipcRenderer.invoke('get-disk-info')
  systemStore.setDiskInfoPanel({
    diskName: '控制盒',
    usedInfo: {
      used: response.used,
      total: response.total
    }
  })
}

async function initData() {
  const systemStore = useSystemStore()
  if (IS_OPEN_AUTHORITY && (import.meta.env.PROD || dev_check_login_status)) {
    await systemStore.getAccountInfoByToken()
  }
  if (shareWindowSetting!['auto_start']) {
    systemStore.startRemoteConnect()
  }
  await startGetDiskInfo()
}

initData()


